package com.marvels.financial.pay.yzfpay;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson15.JSONObject;
import com.fintech.common.enums.ResultCode;
import com.marvels.common.enums.BindStatusEnum;
import com.marvels.common.enums.PayStatusEnum;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.pay.dto.McfePaymentUserBindCardCondition;
import com.marvels.financial.pay.dto.PayResultDto;
import com.upay.sdk.cashier.bindcard.builder.BindCardBuilder;
import com.upay.sdk.cashier.bindcard.builder.BindCardConfirmBuilder;
import com.upay.sdk.cashier.bindcard.builder.BindCardKaptchaBuilder;
import com.upay.sdk.cashier.bindcard.builder.BindCardQueryBuilder;
import com.upay.sdk.cashier.bindcard.builder.UnBindCardBuilder;
import com.upay.sdk.cashier.bindcard.executer.BindCardExecuter;
import com.upay.sdk.cashier.order.builder.ReceiptPaymentBuilder;
import com.upay.sdk.cashier.order.executer.ReceiptOrderExecuter;
import com.upay.sdk.entity.Payer;
import com.upay.sdk.entity.ProductDetail;
import com.upay.sdk.entity.TransferDomesticPayee;
import com.upay.sdk.executer.ResultListenerAdpater;
import com.upay.sdk.onlinepay.builder.OrderBuilder;
import com.upay.sdk.onlinepay.executer.OnlinePayOrderExecuter;
import com.upay.sdk.transferDomestic.builder.QueryBuilder;
import com.upay.sdk.transferDomestic.builder.SingleOrderBuilder;
import com.upay.sdk.transferDomestic.executer.TransferDomesticOrderExecuter;
import com.upay.sdk.transferDomestic.executer.TransferDomesticSingleOrderExecuter;

/**
 * @author helichao
 * @Description 
 * @Date 2021/01/24 16:51:13
 */
@Component
public class YzfPayService {
	
	private final static String CURRENCY_CNY = "CNY";
	private final static String PAYMENT_MODE_CODE = "BANK_CARD-AGREEMENT-P2P";
	private final static String CARD_TYPE = "IDCARD";
	
	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Value("${yzf.pay.callback.url}")
	private String payCallBacKUrl;
	
	/**
	 * 单笔代付
	 * @param orderNo
	 * @param amount
	 * @param accountName
	 * @param bankCardNo
	 * @param bankName
	 * @param bankBranchName
	 * @return
	 */
	public PayResultDto signalPay(String orderNo,String amount,String accountName,String bankCardNo,
			String bankName,String bankBranchName){
		PayResultDto resultDto = new PayResultDto();
		if(StringUtils.isBlank(amount)) {
			resultDto.setPayStatus(PayStatusEnum.FAIL.toString());
			resultDto.setRespMsg("付款金额为空");
			return resultDto;
		}
		String amt = "";
		try {
			amt =String.valueOf(new BigDecimal(amount).multiply(new BigDecimal(100)).longValue());
		}catch(Exception ex) {
			MarvelsLogUtil.getInstance().error("付款金额转换为分失败",ex);
			resultDto.setPayStatus(PayStatusEnum.FAIL.toString());
			resultDto.setRespMsg("付款金额"+amount+"正确");
			return resultDto;
		}
		
		String merchantNo  = getMerchantNo();
		String payInfoStr = "[merchantNo="+merchantNo+"][orderNo="+orderNo+"][amount（分)="+amt+"][accountName="+accountName+"][bankCardNo="+bankCardNo+"][bankName="+bankName+"][bankBranchName="+bankBranchName+"][payCallBacKUrl="+payCallBacKUrl+"]";
		
		TransferDomesticPayee payee = new TransferDomesticPayee();
		payee.setName(accountName);
		payee.setBankCardNum(bankCardNo);
		payee.setBankName(bankName);
		payee.setBranchBankName(bankBranchName);
		SingleOrderBuilder builder = new SingleOrderBuilder(merchantNo);
		builder.setPayee(payee).setAmount(amt).setCurrency(CURRENCY_CNY).setRequestId(orderNo).setRemark("代付")
		.setNotifyUrl(payCallBacKUrl);
		
		TransferDomesticSingleOrderExecuter executer = new TransferDomesticSingleOrderExecuter();
		try{
			MarvelsLogUtil.getInstance().info("开始调用易支付接口，参数为="+ payInfoStr);
			executer.bothOrder(builder, new ResultListenerAdpater(){
			    @Override
	            public void success(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
	
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	String errorMsg = jsonObject.getString("errorMsg");
			    	resultDto.setRespMsg(errorMsg);
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.signalPay result="+jsonObject.toJSONString());
			    	String requestId = jsonObject.getString("requestId");
			    	String status = jsonObject.getString("status");
			    	resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			    	if(StringUtils.equalsIgnoreCase("FAILED", status)) {
			    		resultDto.setPayStatus(PayStatusEnum.FAIL.toString());
			    	}
			    	resultDto.setRespContent(jsonObject.toJSONString());
			    	resultDto.setPlatformOrderNo(requestId);
			    }
			});
		}catch(Exception ex) {
			resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			MarvelsLogUtil.getInstance().error("调用易支付发生异常 "+ payInfoStr, ex);
		}
		MarvelsLogUtil.getInstance().info("调用翼支付单笔代付返回 result="+JSON.toJSONString(resultDto));
		return resultDto;
	}
	
	/**
	 * 代付结果查询
	 * @param orderNo
	 * @return
	 */
	public PayResultDto signalPayQuery(String platformOrderNo){
		String merchantNo  = getMerchantNo();
		MarvelsLogUtil.getInstance().info("YzfPayService.signalPayQuery---[platformOrderNo="+platformOrderNo+"]");

		QueryBuilder builder = new QueryBuilder(merchantNo);
		builder.setRequestId(platformOrderNo);
		
		PayResultDto resultDto = new PayResultDto();
		TransferDomesticSingleOrderExecuter executer = new TransferDomesticSingleOrderExecuter();
		try{
			executer.bothQuery(builder, new ResultListenerAdpater() {
			    @Override
	            public void success(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }

			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	resultDto.setRespCode(ResultCode.fail.getResultCode());
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.signalPayQuery  result="+jsonObject.toJSONString());
			    	String requestId = jsonObject.getString("requestId");
			    	String serialNumber = jsonObject.getString("serialNumber");
			    	String status = jsonObject.getString("status");
			    	resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			    	if(StringUtils.equalsIgnoreCase("FAILED", status)
			    			|| StringUtils.equalsIgnoreCase("CANCEL", status)) {
			    		resultDto.setPayStatus(PayStatusEnum.FAIL.toString());
			    	}else if(StringUtils.equalsIgnoreCase("SUCCESS", status)) {
			    		resultDto.setPayStatus(PayStatusEnum.SUCCESS.toString());
			    	}
			    	resultDto.setRespContent(jsonObject.toJSONString());
			    	resultDto.setPlatformOrderNo(requestId);
			    	resultDto.setPayChannelOrderNo(serialNumber);
			    }
			});
		}
		catch(Exception ex){
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			MarvelsLogUtil.getInstance().error("YzfPayService.signalPayQuery error requestId="+platformOrderNo, ex);
		}
		return resultDto;
	}
	
	/**
	 * 代付结果查询
	 * @param orderNo
	 * @return
	 */
	public PayResultDto payResultCallBackPase(String jsonString){
		MarvelsLogUtil.getInstance().info("回调通知解析结果开始 ，param="+jsonString);
		PayResultDto resultDto = new PayResultDto();
		resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
		JSONObject json = com.alibaba.fastjson15.JSON.parseObject(jsonString);
		TransferDomesticOrderExecuter executer = new TransferDomesticOrderExecuter();
	    try{
		    executer.bothCipherCallback(json, new ResultListenerAdpater() {
		       
			    @Override
	            public void success(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }

			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	resultDto.setRespCode(ResultCode.fail.getResultCode());
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("回调通知解析结果  result="+jsonObject.toJSONString());
			    	String requestId = jsonObject.getString("requestId");
			    	String serialNumber = jsonObject.getString("serialNumber");
			    	String status = jsonObject.getString("status");
			    	resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			    	if(StringUtils.equalsIgnoreCase("FAILED", status)
			    			|| StringUtils.equalsIgnoreCase("CANCEL", status)) {
			    		resultDto.setPayStatus(PayStatusEnum.FAIL.toString());
			    	}else if(StringUtils.equalsIgnoreCase("SUCCESS", status)) {
			    		resultDto.setPayStatus(PayStatusEnum.SUCCESS.toString());
			    	}
			    	resultDto.setRespContent(jsonObject.toJSONString());
			    	resultDto.setPlatformOrderNo(requestId);
			    	resultDto.setPayChannelOrderNo(serialNumber);
			    }
	        });
	    }catch(Exception ex){
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			resultDto.setPayStatus(PayStatusEnum.PAYING.toString());
			MarvelsLogUtil.getInstance().error("回调通知解析结果异常 ，param="+json.toJSONString(), ex);
		}
		return resultDto;
	}
	
	/**
	 * 
	 * @Title: preBindCard 
	 * @Description:发送绑卡请求（即预绑卡，发送短信验证码）    
	 * @param requestno 请求号
	 * @param bankCardNo 银行卡号
	 * @param idno 身份证号码
	 * @param userName 用户姓名
	 * @param phone 手机号码
	 * @date 2018年10月6日 下午5:33:19
	 */
	public McfePaymentUserBindCardCondition preBindCard(String requestNo,String bankCardNo,String idno,
			String userName,String phone){
		
		StringBuilder paramSb = new StringBuilder();
		paramSb.append("[requestNo=")
		       .append(requestNo)
		       .append("][bankCardNo=")
		       .append(bankCardNo)
		       .append("][idno=")
		       .append(idno)
		       .append("][userName=")
		       .append(userName)
		       .append("][phone=").append(phone).append("]");
		MarvelsLogUtil.getInstance().info("YzfPayService.preBindCard param="+paramSb.toString());
		
		String merchantNo  = getMerchantNo();
		BindCardBuilder builder = new BindCardBuilder(merchantNo);
		builder.setMerchantUserId(idno)
				.setBankCardNumber(bankCardNo)
				.setPhoneNumber(phone)
				.setName(userName)
				.setIdCardNum(idno);

		McfePaymentUserBindCardCondition resultDto = new McfePaymentUserBindCardCondition();
		BindCardExecuter executer = new BindCardExecuter();
		try {
			executer.bothBindCard(builder, new ResultListenerAdpater() {
				@Override
				public void success(JSONObject jsonObject) {
					setResultDto(jsonObject);
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	String errorMsg = jsonObject.getString("errorMsg");
			    	resultDto.setRespCode(ResultCode.fail.getResultCode());
			    	resultDto.setRespMsg(errorMsg);
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.preBindCard setResultDto="+jsonObject.toJSONString());
			    	String bankCardNumber = jsonObject.getString("bankCardNumber");
			    	String bindCardId = jsonObject.getString("bindCardId");
			    	String bindStatus = jsonObject.getString("bindStatus");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	if(StringUtils.equalsIgnoreCase("PROCESS", bindStatus)
			    			|| StringUtils.equalsIgnoreCase("INIT", bindStatus)
			    			|| StringUtils.equalsIgnoreCase("SUCCESS", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.PROCESS.toString());
			    	}
			    	resultDto.setBankCardNo(bankCardNumber);
			    	resultDto.setPreBindOrderId(bindCardId);
			    	resultDto.setBindOrderId(bindCardId);
			    	resultDto.setRespContent(jsonObject.toJSONString());
			    }
			});
		} catch (Exception ex) {
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			resultDto.setStatus(BindStatusEnum.FAIL.toString());
			MarvelsLogUtil.getInstance().error("YzfPayService.preBindCard error  param="+paramSb.toString(), ex);
		}
		return resultDto;
	}


	/**
	 * 确认绑卡
	 * @param bindCardId 预绑卡时支付通道返回的唯一id
	 * @param idNo 身份证编号
	 * @param validateCode 短信验证码
	 * @return
	 */
	public McfePaymentUserBindCardCondition bindCard(String bindCardId,String idNo,String validateCode){
		StringBuilder paramSb = new StringBuilder();
		paramSb.append("[bindCardId=")
		       .append(bindCardId)
		       .append("][idNo=")
		       .append(idNo)
		       .append("][validateCode=")
		       .append(validateCode)
		       .append("]");
		MarvelsLogUtil.getInstance().info("YzfPayService.bindCard param="+paramSb.toString());
		
		String merchantNo  = getMerchantNo();
		BindCardConfirmBuilder builder = new BindCardConfirmBuilder(merchantNo);
		builder.setBindCardId(bindCardId)
		       .setMerchantUserId(idNo)
		       .setKaptchaCode(validateCode);;

	    McfePaymentUserBindCardCondition resultDto = new McfePaymentUserBindCardCondition();
		BindCardExecuter executer = new BindCardExecuter();
		try {
			executer.bothBindCardConfirm(builder, new ResultListenerAdpater() {
				@Override
				public void success(JSONObject jsonObject) {
					setResultDto(jsonObject);
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.bindCard result="+jsonObject.toJSONString());
			    	String bankCardNumber = jsonObject.getString("bankCardNumber");
			    	String bindCardId = jsonObject.getString("bindCardId");
			    	String bindStatus = jsonObject.getString("bindStatus");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	if(StringUtils.equalsIgnoreCase("PROCESS", bindStatus)
			    			|| StringUtils.equalsIgnoreCase("INIT", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.PROCESS.toString());
			    	}else if(StringUtils.equalsIgnoreCase("SUCCESS", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.SUCCESS.toString());
			    	}
			    	resultDto.setBankCardNo(bankCardNumber);
			    	resultDto.setPreBindOrderId(bindCardId);
			    	resultDto.setBindOrderId(bindCardId);
			    }
			});
		} catch (Exception ex) {
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			resultDto.setStatus(BindStatusEnum.PROCESS.toString());
			MarvelsLogUtil.getInstance().error("YzfPayService.bindCard error  param="+paramSb.toString(), ex);
		}
		return resultDto;
	}

	/**
	 * 重新发送预绑卡短信 
	 * @param bindCardId 首次预绑卡时支付通道返回的唯一id
	 * @return
	 */
	public McfePaymentUserBindCardCondition preBindCardAgain(String bindCardId){
		StringBuilder paramSb = new StringBuilder();
		paramSb.append("[bindCardId=")
		       .append(bindCardId)
		       .append("]");
		MarvelsLogUtil.getInstance().info("YzfPayService.preBindCardAgain param="+paramSb.toString());

		
		String merchantNo  = getMerchantNo();
		BindCardKaptchaBuilder builder = new BindCardKaptchaBuilder(merchantNo);
		builder.setBindCardId(bindCardId);

		McfePaymentUserBindCardCondition resultDto = new McfePaymentUserBindCardCondition();
		BindCardExecuter executer = new BindCardExecuter();
		try {
			executer.bothBindCardKaptcha(builder, new ResultListenerAdpater() {
				@Override
				public void success(JSONObject jsonObject) {
					setResultDto(jsonObject);
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	String errorMsg = jsonObject.getString("errorMsg");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	resultDto.setRespMsg(errorMsg);
	            }
	            
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.preBindCardAgain result="+jsonObject.toJSONString());
			    	String bindCardId = jsonObject.getString("bindCardId");
			    	resultDto.setPreBindOrderId(bindCardId);
			    	resultDto.setBindOrderId(bindCardId);
			    }
			});
		} catch (Exception ex) {
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			MarvelsLogUtil.getInstance().error("YzfPayService.preBindCardAgain  param="+paramSb.toString(), ex);
		}
		return resultDto;
	}

	/**
	 * 
	 * @Title: queryBindCardRecord 
	 * @Description: 绑卡鉴权记录查询 
	 * @param requestno 请求编号（两参数不能都为空）
	 * @param yborderid 易宝返回编号（两参数不能都为空）
	 * @date 2018年10月6日 下午5:44:45
	 */
	public McfePaymentUserBindCardCondition queryBindCardRecord(String bindCardId,String idNo){
		StringBuilder paramSb = new StringBuilder();
		paramSb.append("[bindCardId=")
		       .append(bindCardId)
		       .append("[idNo=")
		       .append(idNo)
		       .append("]");
		MarvelsLogUtil.getInstance().info("YzfPayService.queryBindCardRecord param="+paramSb.toString());

        //queryBindCardRecord(bindCardId,idNo);
		
		String merchantNo  = getMerchantNo();
		BindCardQueryBuilder builder = new BindCardQueryBuilder(merchantNo);
		builder.setBindCardId(bindCardId).setMerchantUserId(idNo);

		McfePaymentUserBindCardCondition resultDto = new McfePaymentUserBindCardCondition();
		BindCardExecuter executer = new BindCardExecuter();
		try {
			executer.bothBindCardQuery(builder, new ResultListenerAdpater() {
				@Override
				public void success(JSONObject jsonObject) {
					setResultDto(jsonObject);
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
			    	String errorMsg = jsonObject.getString("errorMsg");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	resultDto.setRespMsg(errorMsg);
	            }
	            
			    @Override
	            public void pending(JSONObject jsonObject) {
			    	setResultDto(jsonObject);
	            }
			    
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.queryBindCardRecord result="+jsonObject.toJSONString());
			    	String bankCardNumber = jsonObject.getString("bankCardNumber");
			    	String bindCardId = jsonObject.getString("bindCardId");
			    	String idNo = jsonObject.getString("merchantUserId");
			    	String bindStatus = jsonObject.getString("bindStatus");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	if(StringUtils.equalsIgnoreCase("PROCESS", bindStatus)
			    			|| StringUtils.equalsIgnoreCase("INIT", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.PROCESS.toString());
			    	}else if(StringUtils.equalsIgnoreCase("SUCCESS", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.SUCCESS.toString());
			    	}else if(StringUtils.equalsIgnoreCase("UNBIND", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.UNBIND.toString());
			    	}else if(StringUtils.equalsIgnoreCase("DISABLE", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.DISABLE.toString());
			    	}
			    	resultDto.setIdno(idNo);
			    	resultDto.setBankCardNo(bankCardNumber);
			    	resultDto.setPreBindOrderId(bindCardId);
			    	resultDto.setBindOrderId(bindCardId);
			    }
			});
		} catch (Exception ex) {
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			MarvelsLogUtil.getInstance().error("YzfPayService.queryBindCardRecord  param="+paramSb.toString(), ex);
		}
		return resultDto;
	} 

	/**
	 * 解绑接口
	 * @param idno 
	 * @param bindCardId
	 * @return
	 */
	public McfePaymentUserBindCardCondition unbind(String bindCardId,String idNo){
		StringBuilder paramSb = new StringBuilder();
		paramSb.append("[bindCardId=")
		       .append(bindCardId)
		       .append("[idNo=")
		       .append(idNo)
		       .append("]");
		MarvelsLogUtil.getInstance().info("YzfPayService.unbind param="+paramSb.toString());
		
		String merchantNo  = getMerchantNo();
		UnBindCardBuilder builder = new UnBindCardBuilder(merchantNo);
		builder.setBindCardId(bindCardId).setMerchantUserId(idNo);

		McfePaymentUserBindCardCondition resultDto = new McfePaymentUserBindCardCondition();
		BindCardExecuter executer = new BindCardExecuter();
		try {
			executer.bothUnbindCard(builder, new ResultListenerAdpater() {
				@Override
				public void success(JSONObject jsonObject) {
					setResultDto(jsonObject);
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
			    	String errorMsg = jsonObject.getString("errorMsg");
			    	resultDto.setStatus(BindStatusEnum.FAIL.toString());
			    	resultDto.setRespMsg(errorMsg);
	            }
	            
			    private void setResultDto(JSONObject jsonObject) {
			    	MarvelsLogUtil.getInstance().info("YzfPayService.unbind result="+jsonObject.toJSONString());
			    	String bankCardNumber = jsonObject.getString("bankCardNumber");
			    	String bindCardId = jsonObject.getString("bindCardId");
			    	String idNo = jsonObject.getString("merchantUserId");
			    	String bindStatus = jsonObject.getString("bindStatus");
			    	if(StringUtils.equalsIgnoreCase("UNBIND", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.UNBIND.toString());
			    	}else if(StringUtils.equalsIgnoreCase("DISABLE", bindStatus)) {
			    		resultDto.setStatus(BindStatusEnum.DISABLE.toString());
			    	}
			    	resultDto.setIdno(idNo);
			    	resultDto.setBankCardNo(bankCardNumber);
			    	resultDto.setPreBindOrderId(bindCardId);
			    	resultDto.setBindOrderId(bindCardId);
			    }
			});
		} catch (Exception ex) {
			resultDto.setRespCode(ResultCode.fail.getResultCode());
			MarvelsLogUtil.getInstance().error("YzfPayService.unbind param="+paramSb.toString(), ex);
		}
		return resultDto;
	}	
	
	/**
	 * @param requestNo
	 * @param idNo
	 * @param bankCardNo
	 * @param amount
	 * @param accountName
	 * @param phone
	 * @param title
	 * @param bindCardId
	 * @return
	 */
	public Map<String,Object> withhold(String requestNo,String idNo,String bankCardNo,String amount,
			String accountName,String phone,String bindCardId){
		OrderBuilder builder = new OrderBuilder(getMerchantNo());
		builder.setRequestId(requestNo).setOrderAmount(amount).setOrderCurrency(CURRENCY_CNY)
				.setNotifyUrl("").setCallbackUrl("").setRemark("收款")
				.setPaymentModeCode(PAYMENT_MODE_CODE).setMerchantUserId(idNo)
				.setBindCardId(bindCardId).setClientIp(getServerIp());
		builder.setTimeout(getTimeOut());
		
		ProductDetail productDetail = new ProductDetail();
		productDetail.setName("服务费");
		productDetail.setQuantity(1L);
		productDetail.setAmount(Long.parseLong(amount));
		builder.addProductDetail(productDetail);
		
		Payer payer = new Payer();
		payer.setName(accountName);
		payer.setPhoneNum(phone);
		payer.setBankCardNum(bankCardNo);
		payer.setIdNum(idNo);
		payer.setIdType(CARD_TYPE);
		builder.setPayer(payer);
 
		try{
            OnlinePayOrderExecuter executer = new OnlinePayOrderExecuter();
			executer.bothOrder(builder, new ResultListenerAdpater() {
                /**
                                                    *  提交成功，不代表支付成功
                 */
				@Override
                public void success(JSONObject jsonObject) {
					
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
	                String result = jsonObject.toJSONString();
	            }
            });
		}
		catch(Exception e){
				
		}
		return null;
	}
	
	/**
	 * 如果代扣请求接口返回发送验证码，则需要调用此接口确认扣款
	 * @param requestNo
	 * @param kaptchaCode
	 * @param paymentOrderId
	 * @return
	 */
	public Map<String,Object> withholdConfirm(String requestNo,String kaptchaCode,String paymentOrderId){
		ReceiptPaymentBuilder builder = new ReceiptPaymentBuilder(getMerchantNo());
		builder.setRequestId(requestNo).setPaymentOrderId(paymentOrderId).setKaptchaCode(kaptchaCode);
		try{
			ReceiptOrderExecuter executer = new ReceiptOrderExecuter();
			executer.payment(builder, new ResultListenerAdpater() {
                /**
                 * 提交成功，不代表支付成功
                 */
				@Override
                public void success(JSONObject jsonObject) {
					
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
	                String result = jsonObject.toJSONString();
	            }
            });
		}
		catch(Exception e){
				
		}
		return null;
	}
	
	/**
	 * 代扣接口查询
	 * @param requestNo
	 * @param paymentOrderId
	 * @return
	 */
	public Map<String,Object> withholdResultQuery(String requestNo,String paymentOrderId){
		com.upay.sdk.onlinepay.builder.QueryBuilder builder = new com.upay.sdk.onlinepay.builder.QueryBuilder(getMerchantNo());
		builder.setRequestId(requestNo);
		try{
			OnlinePayOrderExecuter executer = new OnlinePayOrderExecuter();
			executer.bothQuery(builder, new ResultListenerAdpater() {
                /**
                 * 提交成功，不代表支付成功
                 */
				@Override
                public void success(JSONObject jsonObject) {
					
				}
				
			    @Override
	            public void failure(JSONObject jsonObject) {
	                String result = jsonObject.toJSONString();
	            }
			    
				@Override
	            public void pending(JSONObject jsonObject) {
	                String result = jsonObject.toJSONString();
	            }
            });
		}catch(Exception e){
				
		}
		return null;
	}
	
	private String getMerchantNo() {
//		return "890000593";
		return ServerProperties.getValue(configPropertiesPath, "common.properties", "yzf.merchantno.pay");
	}
	
	private String getServerIp() {
//		return "890000593";
		return ServerProperties.getValue(configPropertiesPath, "common.properties", "server.ip");
	}
	
	private String getTimeOut() {
//		return "30";
		return ServerProperties.getValue(configPropertiesPath, "common.properties", "yzf.timeout");
	}
	
	public static void main(String[] args) {
		
		YzfPayService service = new YzfPayService();
//		McfePaymentUserBindCardCondition dto = service.preBindCard(String.valueOf(System.currentTimeMillis()), "6222980060592781", "421081198511200615", "何立超", "18666224263");
//		McfePaymentUserBindCardCondition dto = service.bindCard("20210125507692538480926729293824", "421081198511200615", "977689");
//		McfePaymentUserBindCardCondition dto = service.queryBindCardRecord("20210125507692538480926729293824", "421081198511200615");
		McfePaymentUserBindCardCondition dto = service.unbind("20210127035229539158354095099904", "421081198511200615");
//		TransferDomesticPayee payee = new TransferDomesticPayee();
//		payee.setName("test");
//		payee.setBankCardNum("622298001230987");
//		payee.setBankName("");
//		payee.setBranchBankName("");
//		SingleOrderBuilder builder = new SingleOrderBuilder("890000595");
//		builder.setPayee(payee).setAmount("100").setCurrency(CURRENCY_CNY).setRequestId("jdklsafjsa").setRemark("代付");
		System.out.println(JSON.toJSONString(dto));
	}
	
}
